#include "stdafx.h"
#include "wf_drawlinehorvert.h"
#include "pf_snapper.h"



WF_DrawLineHorVert::WF_DrawLineHorVert(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw horizontal/vertical lines",
                           container, graphicView) {
    reset();
    PF_DEBUG->print("PF_ActionDrawLineHorVert::constructor");
}



WF_DrawLineHorVert::~WF_DrawLineHorVert() {}


QAction* WF_DrawLineHorVert::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
/*      QAction* action = new QAction(tr("hor./vert. line"),
                                  tr("H&orizontal / Vertical"),
                                  QKeySequence(), NULL); */
    QAction* action = new QAction(tr("Vertical"), NULL);
    //action->zetStatusTip(tr("Draw horizontal/vertical lines"));
    return action;
}

void WF_DrawLineHorVert::reset() {
    data = PF_LineAttribute(PF_Vector(false),
                       PF_Vector(false));
}



void WF_DrawLineHorVert::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
    PF_DEBUG->print("PF_ActionDrawLineHorVert::init");
}



void WF_DrawLineHorVert::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_Line* line = new PF_Line(container, data);
    line->setLayerToActive();
    line->setPenToActive();
    container->addEntity(line);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(line);
        document->endUndoCycle();
    }

	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(line->getMiddlepoint());
    PF_DEBUG->print("PF_ActionDrawLineHorVert::trigger():"
                    " line added: %d", line->getId());

}



void WF_DrawLineHorVert::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLineHorVert::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);
    if (getStatus()==SetEndpoint && p1.valid) {
        PF_Vector p2x = PF_Vector(mouse.x, p1.y);
        PF_Vector p2y = PF_Vector(p1.x, mouse.y);
        if (mouse.distanceTo(p2y) > mouse.distanceTo(p2x))
            p2 = p2x;
        else
            p2 = p2y;
        deletePreview();
        data = PF_LineAttribute(p1, p2);
        preview->addEntity(new PF_Line(preview, data));
        drawPreview();
    }

    PF_DEBUG->print("PF_ActionDrawLineHorVert::mouseMoveEvent end");
}



void WF_DrawLineHorVert::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_Vector mouse = snapPoint(e);

        switch (getStatus()) {
        case SetStartpoint:
            p1 = mouse;
            setStatus(SetEndpoint);
            break;

        case SetEndpoint:
            p2 = mouse;
            trigger();
            setStatus(SetStartpoint);
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawLineHorVert::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetStartpoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"),
                                            tr("Cancel"));
        break;
    case SetEndpoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}


void WF_DrawLineHorVert::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}


void WF_DrawLineHorVert::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}

// EOF
